package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.Strategy;
import cn.edu.gcu.giftems.service.StrategyService;
import cn.edu.gcu.giftems.utils.PageUtils;
import cn.edu.gcu.giftems.utils.export.StrategyExportUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class StrategyAction extends ActionSupport implements ModelDriven<Strategy> {

    private StrategyService strategyService =
            (StrategyService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("strategyService");
    private Strategy strategy = new Strategy();
    private List<Strategy> list;

    @Override
    public Strategy getModel() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public void setStrategyService(StrategyService strategyService) {
        this.strategyService = strategyService;
    }

    public List<Strategy> getList() {
        return list;
    }

    public void setList(List<Strategy> list) {
        this.list = list;
    }

    /**
     * 分页查询攻略的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<Strategy> pageUtils = strategyService.findByPage(currPage);
        // ActionContext.getContext().getValueStack().push(pageUtils);
        pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        //list = pageUtils.getList();
        return "success";
    }

    /**
     * 添加攻略的执行方法
     */
    public String save(){
        strategyService.save(strategy);
        return this.findByPage();
    }

    /**
     * 更新攻略信息的执行方法
     */
    public String update(){
        if (strategy.getId() > 0)
            strategyService.update(strategy);
        return this.findByPage();
    }
    /**
     * 根据攻略id删除攻略
     */
    public String delete(){
        strategy = strategyService.findById(strategy.getId());
        if (strategy != null)
            strategyService.delete(strategy);
        return this.findByPage();
    }

    public void export()
    {
        StrategyExportUtils Export=new StrategyExportUtils();
        try {
            Export.export(strategyService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String findByParameter() {
        String filter_name = ActionContext.getContext().getParameters().get("filter-name").toString();
        String simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value").toString();
        PageUtils<Strategy> pageUtils = strategyService.findByParameter(currPage, filter_name,simple_filter_value);
        ActionContext.getContext().put("pageUtils", pageUtils);
        return "success";
    }
}
